package com.xiaoqi.api.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoqi.api.dto.ProjectDTO;
import com.xiaoqi.api.mapper.ProjectMapper;
import com.xiaoqi.api.query.ProjectQuery;
import com.xiaoqi.api.service.ProjectService;
import com.xiaoqi.api.utlis.PageResult;
import com.xiaoqi.api.vo.ProjectVO;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class ProjectServiceImp implements ProjectService {

    @Resource
    ProjectMapper projectMapper;


    @Override
    public PageResult<ProjectVO> find(ProjectQuery projectQuery) {
//设置是否需要分页
        PageHelper.startPage(projectQuery.getPage(),projectQuery.getPageSize());  //告诉PageHelper此处需要分页

        Page<ProjectVO> page = projectMapper.find(projectQuery);

        PageInfo<ProjectVO> pageInfo = page.toPageInfo();

        List<ProjectVO> pageInfoList = pageInfo.getList();

        long total = pageInfo.getTotal();

        PageResult<ProjectVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(pageInfoList);
        return pageResult;
    }

    @Override
    public void add(ProjectDTO projectDTO) {
        projectMapper.add(projectDTO);
    }

    @Override
    public void update(Integer id, ProjectDTO projectDTO) {
        projectMapper.update(id,projectDTO);
    }

    @Override
    public ProjectVO findById(Integer id) {
        return projectMapper.findById(id);
    }
}
